package com.example.myzhugezhaofang.fragment.consultingfrag;

import android.os.Bundle;
import android.util.Log;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.mvplibrary.ui.fragment.BaseMvpFragment;
import com.example.myzhugezhaofang.R;
import com.example.myzhugezhaofang.adapter.FragTabAdapter;
import com.example.myzhugezhaofang.bean.NewsTypeListBean;
import com.example.myzhugezhaofang.callback.Mainconcations;
import com.example.myzhugezhaofang.model.MainModel;
import com.example.myzhugezhaofang.presenter.MainPresenter;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.google.gson.JsonElement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：朱勇闯
 * data : 2021/4/16  11:42
 * 作用： 资讯下市场报告
 */
public class MarketDynamicFragment<T> extends BaseMvpFragment<Mainconcations.MainView, MainModel, MainPresenter<T>> implements Mainconcations.MainView {
    private static final String TAG = "朱勇闯MarketDynamic";
    @BindView(R.id.tab_consulting_marketdynamic)
    TabLayout tab;
    @BindView(R.id.vp_consulting_marketdynamic)
    ViewPager viewPager;
    private List<NewsTypeListBean.ListBean.ChildBean> childList;

    @Override
    protected MainPresenter<T> createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initEvent() {
        HashMap<String, Object> TypeMap = new HashMap<>();
        TypeMap.put("platformType", "4");
        mPresenter.getData(TypeMap, "https://api.zhuge.com//contentService/v1/News/", "appNewsTypeList", 0);
    }

    @Override
    protected int createLayoutId() {
        return R.layout.fragment_consulting_marketdynamic;
    }

    @Override
    public void showList(Object t, int type) {
        if (type == 0) {
            ArrayList<String> string = new ArrayList<>();
            NewsTypeListBean newsTypeListBean = new Gson().fromJson((JsonElement) t, NewsTypeListBean.class);

            childList = newsTypeListBean.getList().get(3).getChildList();
//            Log.e(TAG, "showList: " + childList.get(0).getTitle());
            for (int i = 0; i < childList.size(); i++) {
                string.add(childList.get(i).getTitle());
            }
            ArrayList<Fragment> fragments = new ArrayList<>();
            Log.e(TAG, "showList: string.size" + string.size());
            for (int i = 0; i < string.size(); i++) {
                Bundle bundle = new Bundle();
                MarketDynamicChildFragment dynamicChildFragment = new MarketDynamicChildFragment();
                bundle.putString("secondModular", childList.get(i).getSecondModular());
                dynamicChildFragment.setArguments(bundle);
                Log.e(TAG, "showList: " + childList.get(i).getSecondModular());
                fragments.add(dynamicChildFragment);
            }
//            Log.e(TAG, "showList: fragments.size" + fragments.size());
            FragTabAdapter adapter = new FragTabAdapter(getChildFragmentManager(), fragments, string);
            viewPager.setAdapter(adapter);
            tab.setupWithViewPager(viewPager);
        }
    }

    @Override
    public void showError(String error) {
        Log.e(TAG, "showError: " + error);
    }

    @Override
    public void showProgressbar() {

    }

    @Override
    public void hideProgressbar() {

    }
}
